<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function ingresar()
	{
                $data['main_content'] = 'invitado/login';
		$this->load->view('invitado/template',$data);
	}
        
        public function logueo()
	{
            
                
            $this->load->database();    
            
            $this->load->model('usuariomodel');
            
            $this->load->library('session');
            
             if((isset($_POST["username"])==true) && (isset($_POST["password"])==true))
            {
                $newdata = array(
                    'username' => $_POST["username"],
                    'password' => $_POST["password"],               
                    'logged_in' => TRUE
                );
                  $this->session->set_userdata($newdata);
            }
            $username = $this->session->userdata('username');
            
            $password = $this->session->userdata('password');
                
           $resultado= $this->usuariomodel->loguear($username, $password);
            
           if($resultado==1)
           {
               
                $nivel= $this->usuariomodel->nivel($username, $password);
               
                if($nivel==2)
                {
                    
                     $data['main_content'] = 'investigador/index';
	
                     $this->load->view('investigador/template',$data);
                }
                if($nivel==1)
                {
                    $data['main_content'] = 'administrador/index';
	
                     $this->load->view('administrador/template',$data);
                }
           }
           else
           {
                  $data['main_content'] = 'invitado/login';
	
                  $this->load->view('invitado/template',$data);
           }
         
	}
}
